package com.gitee.cui.security.netty.https;

import com.gitee.cui.http.HttpProtocolHelper;
import com.gitee.cui.util.JsonUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.QueryStringDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;

/**
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/11/5 17:00
 **/
@Slf4j
public class HttpEchoHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
        if (!request.decoderResult().isSuccess()) {
            HttpProtocolHelper.sendError(ctx, HttpResponseStatus.BAD_REQUEST);
            return;
        }

        // 缓存http协议的版本号
        HttpProtocolHelper.cacheHttpProtocol(ctx, request);

        Map<String, Object> echo = new HashMap<>();
        String uri = request.uri();
        echo.put("uri: ", uri);

        HttpMethod requestMethod = request.method();
        echo.put("requst method: ", requestMethod.toString());

        // 解析请求参数
        Map<String, Object> datas = paramsFromGet(request);
        echo.put("paramsFromGet", datas);

        // 转换成json字符串
        String sendContent = JsonUtil.pojoToJson(echo);

        // 发送请求内容
        HttpProtocolHelper.sendJsonContent(ctx, sendContent);
    }

    /*
     * 获取GET方式传递的参数
     */
    private Map<String, Object> paramsFromGet(FullHttpRequest fullHttpRequest) {

        Map<String, Object> params = new HashMap<>();

        if (fullHttpRequest.method() == HttpMethod.GET) {
            // 处理get请求，把URI分割成key-value形式
            QueryStringDecoder decoder = new QueryStringDecoder(fullHttpRequest.uri());
            Map<String, List<String>> paramList = decoder.parameters();
            // 迭代处理请求参数列表
            for (Map.Entry<String, List<String>> entry : paramList.entrySet()) {
                params.put(entry.getKey(), entry.getValue().get(0));
            }
            return params;
        } else {
            return null;
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.info("远程连接已经主动关闭， channel {}", ctx.channel());
        if (ctx.channel().isActive()) {
            HttpProtocolHelper.sendError(ctx, INTERNAL_SERVER_ERROR);
        }
    }
}
